package com.elric.mall.service.impl;

import com.elric.mall.common.utils.ImportExcelUtil;
import com.elric.mall.dto.KzNewProjectImportDto;
import com.elric.mall.enums.ImportType;
import com.elric.mall.mapper.KzNewProjectMapper;
import com.elric.mall.model.KzNewProject;
import com.elric.mall.model.KzNewProjectExample;
import com.elric.mall.service.KzNewProjectService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.List;

/**
 * 实现功能：
 *
 * @author 作者[Yuxuan.he]
 * @version 1.0.00  2021-09-16 11:21
 */
@Service
public class KzNewProjectServiceImpl implements KzNewProjectService {
    @Autowired
    private KzNewProjectMapper kzNewProjectMapper;

    @Override
    public int create(KzNewProject kzNewProject) {
        return kzNewProjectMapper.insert(kzNewProject);
    }

    @Override
    public int update(KzNewProject kzNewProject) {
        return kzNewProjectMapper.updateByPrimaryKeySelective(kzNewProject);
    }

    @Override
    public KzNewProject findById(Long id) {
        return kzNewProjectMapper.selectByPrimaryKey(id);
    }

    @Override
    public int delete(Long id) {
        return kzNewProjectMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<KzNewProject> list(Integer pageSize, Integer pageNum, KzNewProject kzNewProject) {
        PageHelper.startPage(pageNum, pageSize);
        KzNewProjectExample example = new KzNewProjectExample();
        if(!StringUtils.isEmpty(kzNewProject.getBuildUnitName())) {
            example.createCriteria().andBuildUnitNameLike("%"+kzNewProject.getBuildUnitName()+ "%");
        }
        if(!StringUtils.isEmpty(kzNewProject.getProjectName())) {
            example.createCriteria().andProjectNameLike("%" + kzNewProject.getProjectName() + "%");
        }
        return kzNewProjectMapper.selectByExample(example);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<KzNewProjectImportDto> importDocuments(MultipartHttpServletRequest request) {
        return ImportExcelUtil.readExcelByRequest(request, new KzNewProjectImportDto());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertBatch(List<KzNewProject> list, ImportType importType) {
        if (importType == ImportType.full) {
            KzNewProjectExample example = new KzNewProjectExample();
            kzNewProjectMapper.deleteByExample(example);
        }
        return kzNewProjectMapper.insertBatch(list);
    }
}
